﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using DAL;
using CTRL;

namespace GUI_Client
{
    public partial class TakeBatteries : Form
    {
        private static TakeBatteries instance;
        private List<Booking> bookingList;
        private CtrlBooking bookCtrl;
        private CtrlBookingItem bookItemCtrl;
        private CtrlBatteryItem batItemCtrl;
        private CtrlBatteryStation batStatCtrl;

        private TakeBatteries()
        {
            bookCtrl = CtrlBooking.Instance;
            bookItemCtrl = CtrlBookingItem.Instance;
            batItemCtrl = CtrlBatteryItem.Instance;
            batStatCtrl = CtrlBatteryStation.Instance;
            this.bookingList = bookCtrl.GetAllBookingsOfCustomer(GUIMain.Customer.customerID);

            InitializeComponent();
            ShowTable();
        }

        public static TakeBatteries Instance
        {
            get
            {
                if (instance == null)
                    instance = new TakeBatteries();

                return instance;
            }
        }

        private void ShowTable()
        {
            table.Rows.Clear();
            foreach (Booking book in bookingList)
            {
                List<BookingItem> items = bookItemCtrl.GetBookingItemsFromBooking(book.bookingID).ToList();
                foreach (BookingItem item in items)
                {
                    BatteryItem bi = batItemCtrl.GetBatteryItem(item.batteryID);
                    BatteryStation bs = batStatCtrl.GetBatteryStationByID(bi.stationID);
                    if (bi.state == BatteryState.reserved.ToString())
                        table.Rows.Add(book.bookingID, item.batteryID, bs.name);
                }
            }
            if (table.Rows.Count == 0)
            {
                lbl.Visible = true;
                takeBtn.Visible = false;
            }
        }

        private void takeBtn_Click(object sender, EventArgs e)
        {
            batItemCtrl.UpdateBatteryState(batItemCtrl.GetBatteryItem(int.Parse(
                table.CurrentRow.Cells[1].Value.ToString())), BatteryState.taken);
            MessageBox.Show("You have taken the battery", "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ShowTable();
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIBookings bookings = GUIBookings.Instance;
            bookings.Show();

            this.Hide();
        }
    }
}